package com.example.springbootdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * Worker Bean ID 是 worker
 */
@Component
public class Worker {
    private String name = "光头强";

    /**
     * Spring 会自动为Worker 注入 saw 对象: 此种方式称为依赖注入
     */
    //@Autowired
    //Saw saw;
    //@Autowired
//    public Worker(Saw saw){
//        this.saw = saw;
//        System.out.println("使用构造器注入 saw");
//    }
//
//    @Autowired
//    public void abc(Saw saw) {
//        this.saw = saw;
//        System.out.println("abc 注入 saw");
//    }


    private Tool tool;
    // 如果类型匹配时候, 找到多个组件, 则安装Bean ID匹配 tool变量名!
    @Autowired
    public Worker(@Qualifier("AXE") Tool tool){
        this.tool = tool;
    }

    /**
     * 工作方法,体现了 在砍树之前必须依赖 saw
     */
    public void work(){
        System.out.println(name + "使用" + tool + "砍树");
    }

    @Override
    public String toString() {
        return "Worker{" +
                "name='" + name + '\'' +
                ", tool=" + tool +
                '}';
    }
}
